package org.jeecg.modules.business.repertory.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.business.repertory.entity.WmsRepertoryLockHistory;
import org.jeecg.modules.business.repertory.mapper.WmsRepertoryMapper;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryLockHistoryService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 库存-锁库日志
 * @Author: jeecg-boot
 * @Date: 2023-08-01
 * @Version: V1.0
 */
@Api(tags = "库存-锁库日志")
@RestController
@RequestMapping("/repertory/wmsRepertoryLockHistory")
@Slf4j
public class WmsRepertoryLockHistoryController extends JeecgController<WmsRepertoryLockHistory, IWmsRepertoryLockHistoryService> {
    @Autowired
    private IWmsRepertoryLockHistoryService wmsRepertoryLockHistoryService;
    @Autowired
    private IWmsRepertoryService wmsRepertoryService;
    @Autowired
    private WmsRepertoryMapper repertoryMapper;

    /**
     * 分页列表查询
     *
     * @param wmsRepertoryLockHistory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "库存-锁库日志-分页列表查询", notes = "库存-锁库日志-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsRepertoryLockHistory>> queryPageList(WmsRepertoryLockHistory wmsRepertoryLockHistory,
                                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                HttpServletRequest req) {
        QueryWrapper<WmsRepertoryLockHistory> queryWrapper = QueryGenerator.initQueryWrapper(wmsRepertoryLockHistory, req.getParameterMap());
        Page<WmsRepertoryLockHistory> page = new Page<WmsRepertoryLockHistory>(pageNo, pageSize);
        IPage<WmsRepertoryLockHistory> pageList = wmsRepertoryLockHistoryService.page(page, queryWrapper);
        List<String> list = pageList.getRecords().stream().map(WmsRepertoryLockHistory::getWmsRepertoryId).collect(Collectors.toList());
        List<WmsRepertory> wmsRepertoryList = repertoryMapper.listRepertory(list);
        Map<String, WmsRepertory> collect = wmsRepertoryList.stream().collect(Collectors.toMap(o -> o.getId(), o->o));
        pageList.getRecords().forEach(one -> {
            WmsRepertory repertory = collect.get(one.getWmsRepertoryId());
            if (repertory != null) {
                one.setWmsBasicWarehouseId(repertory.getWmsBasicWarehouseId());
                one.setWmsBasicWarehouseAreaId(repertory.getWmsBasicWarehouseAreaId());
                one.setWmsBasicWarehouseAreaLocationId(repertory.getWmsBasicWarehouseAreaLocationId());
                one.setWmsBasicUnitId(repertory.getWmsBasicUnitId());
                one.setStockOrg(repertory.getStockOrg());
                one.setMaterialCode(repertory.getMaterialCode());
                one.setMaterialName(repertory.getMaterialName());
                one.setUnitSub(repertory.getUnitSub());
                one.setSpecification(repertory.getSpecification());
                one.setBatchCode(repertory.getBatchCode());
            }
        });
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmsRepertoryLockHistory
     * @return
     */
    @AutoLog(value = "库存-锁库日志-添加")
    @ApiOperation(value = "库存-锁库日志-添加", notes = "库存-锁库日志-添加")
    @RequiresPermissions("repertory:wms_repertory_lock_history:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmsRepertoryLockHistory wmsRepertoryLockHistory) {
        wmsRepertoryLockHistoryService.save(wmsRepertoryLockHistory);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmsRepertoryLockHistory
     * @return
     */
    @AutoLog(value = "库存-锁库日志-编辑")
    @ApiOperation(value = "库存-锁库日志-编辑", notes = "库存-锁库日志-编辑")
    @RequiresPermissions("repertory:wms_repertory_lock_history:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsRepertoryLockHistory wmsRepertoryLockHistory) {
        wmsRepertoryLockHistoryService.updateById(wmsRepertoryLockHistory);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存-锁库日志-通过id删除")
    @ApiOperation(value = "库存-锁库日志-通过id删除", notes = "库存-锁库日志-通过id删除")
    @RequiresPermissions("repertory:wms_repertory_lock_history:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmsRepertoryLockHistoryService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "库存-锁库日志-批量删除")
    @ApiOperation(value = "库存-锁库日志-批量删除", notes = "库存-锁库日志-批量删除")
    @RequiresPermissions("repertory:wms_repertory_lock_history:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsRepertoryLockHistoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "库存-锁库日志-通过id查询")
    @ApiOperation(value = "库存-锁库日志-通过id查询", notes = "库存-锁库日志-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsRepertoryLockHistory> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsRepertoryLockHistory wmsRepertoryLockHistory = wmsRepertoryLockHistoryService.getById(id);
        if (wmsRepertoryLockHistory == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsRepertoryLockHistory);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsRepertoryLockHistory
     */
    @RequiresPermissions("repertory:wms_repertory_lock_history:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsRepertoryLockHistory wmsRepertoryLockHistory) {
        return super.exportXls(request, wmsRepertoryLockHistory, WmsRepertoryLockHistory.class, "库存-锁库日志");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("repertory:wms_repertory_lock_history:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsRepertoryLockHistory.class);
    }

}
